//
//  ZZBaseNavController.swift
//  sina
//
//  Created by 张鹏 on 16/5/22.
//  Copyright © 2016年 ZP. All rights reserved.
//

import UIKit

class ZZBaseNavController: UINavigationController ,UIGestureRecognizerDelegate{

   override func viewDidLoad() {
        super.viewDidLoad()
    
    //解决右滑返回手势的问题
    //存在bug 在最顶级的页面 右滑返回手势还会响应
    self.interactivePopGestureRecognizer?.delegate = self
    
    }
    
    //重写push方法
    //ZZBaseNavController(rootViewController: vc) ==> 内部会进行push操作 ==> 压栈
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //当控制器将要跳转的时候
        
        var backTitle = "返回"
        
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1 {
                //返回按钮的title = 最上层页面得title
                backTitle = childViewControllers.first?.title ?? "返回"
            }
                let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(popToFrontVC))
            
            viewController.navigationItem.leftBarButtonItem = back
            
        }
        
        super.pushViewController(viewController, animated: animated)
       
    }
    
//    private lazy var aa = UIImageView(image: 
    
    @objc private   func popToFrontVC() {
        
        popViewControllerAnimated(true)
    }
    
    
    
    //实现手势协议方法
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
        
    }
    
    
}
